const http = require("http");
const fs = require('fs');
const server = http.createServer((req, res) => {
let {pathname} = new URL(req.url,'http://localhost:8181')
if(pathname === '/'){
    pathname = '/index.html'
}
console.log(__dirname)
const publicDir = __dirname + '/public'
const filePath = publicDir + pathname 
fs.readFile(filePath,(error,data)=>{
    if (error) { // 没有对应的文件
        res.end('<h1>404 Not Found</h1>')
      } else {
        res.end(data)
      }
    })
})
    
server.listen(8181, () => {
    console.log('启动服务成功: http://localhost:8181')
  })

